# @Time    :2025/4/7 10:36
""" 多进程方式读写文件 """
import multiprocessing
import time


# 向文件中写入数据
def write_file():
    with open("test.txt", "a") as f:
        while True:
            f.write("hello world\n")
            f.flush()#缓冲一下
            time.sleep(0.5)


# 向文件读取数据
def read_file():
    with open("test.txt", "r") as f:
        while True:
            time.sleep(0.5)
            f.readline(1)


if __name__ == "__main__":
    p1 = multiprocessing.Process(target=write_file)#target= :当前进程执行的任务
    p2 = multiprocessing.Process(target=read_file)
    p1.start()
    p2.start()
